function calculator() {
	
}
calculator.prototype = {
	constructor: calculator,
	location_type: "localstorage",
	save_location: "history_array",
	//数据拆分 参数是字符串
	data_split: function(var1) {
		return var1.split(' ');
	},
	//数据检验 传入的是数组
	data_checking: function(var1) {
		var arr = new Array("+", "-", "*", "/");
		try {
			for(var i = 0; i < var1.length; i++) {
				if(i % 2 == 1) {
					if(arr.some(function(item, index, array) {
							return item == var1[i];
						})) {
						continue;
					} else throw "输入格式错误";
				}
				if(i % 2 == 0) {
					if(var1[i] > '9' || var1[i] < '0') throw "输入格式错误";
				}
			}
		} catch(e) {
			//TODO handle the exception
			//			console.log(e);
			this.show_result("error:" + e);
			throw new Error(e);
		}
	},
	//计算  传入的是检验过的格式正确的数组
	result_calculate: function() {
		var data_arr = this.data_split(this.content);
		var arr = new Array();
		var last, front;
		//从后到前 只运算乘除，每次乘除运算完毕pop到另外一个数组中
		for(var i = data_arr.length - 1; i >= 0;) {
			if(i == 0) {
				arr.push(data_arr[i--]);
				break;
			}
			switch(data_arr[i - 1]) {
				case '+':
					arr.push(data_arr[i--]);
					arr.push(data_arr[i--]);
					break;
				case '-':
					arr.push(data_arr[i--]);
					arr.push(data_arr[i--]);
					break;
				case '*':
					last = parseFloat(data_arr[i--]);
					front = parseFloat(data_arr[--i]);
					data_arr[i] = front * last;
					break;
				case '/':
					try {
						last = parseFloat(data_arr[i--]);
						front = parseFloat(data_arr[--i]);
						if(last == 0) throw "被除数不能为0"; //除数为零抛出异常，在屏幕上提示相关信息
						data_arr[i] = front / last;
					} catch(e) {
						this.show_result(e);
						throw new Error(e);
					}
					break;
			}
		}
		//颠倒顺序 从后到钱 将最后得到的只含加减的数组再次运算
		arr.reverse();
		for(var i = arr.length - 1; i >= 0;) {
			if(i == 0) {
				front = parseFloat(arr[0]);
				break;
			}
			last = parseFloat(arr[i--]);
			front = parseFloat(arr[--i]);
			if(arr[i + 1] == '+') {
				front += last;
			} else {
				front -= last;
			}
			arr[i] = front;
		}

		this.data_store($("#result").text() + " = " + front); //数据存储
		temp = front; //方便在这次计算的结果上再次计算
		return front;
	},
	//解析json数据  传入的是一串json数据
	data_analysis: function(var1) {
		var arr;
		if(var1 != null) {
			arr = JSON.parse(var1);
		} else {
			arr = new Array();
		}
		return arr;
	},
	//数据存储
	data_store: function(var1) {
		var history_array = this.data_analysis(localStorage.getItem(this.save_location));
		var temp = new Object();
		temp.result = var1;
		history_array.push(temp);
		if(this.location_type == "localstorage")
			localStorage.setItem(this.save_location, JSON.stringify(history_array));
	},
	//结果显示
	show_result: function(var1) {
		$("#result").text(var1); //将结果显示
	}
}

Object.defineProperties(calculator.prototype, {
	_content:{
		writeable:true,
		value:''
	},
	content: {
		set: function(newvalue) {
			var data_arr = this.data_split(newvalue);
			this.data_checking(data_arr);
			this._content = data_arr;

		},
		get: function() {
			return this._content;
		}
	}
});

